package cn.fntop.weixin.controller;

import cn.fntop.weixin.annotation.FnMsg;
import com.jfinal.weixin.sdk.api.CustomServiceApi;
import com.jfinal.weixin.sdk.msg.in.*;
import com.jfinal.weixin.sdk.msg.in.event.InFollowEvent;
import com.jfinal.weixin.sdk.msg.in.event.InMenuEvent;
import com.jfinal.weixin.sdk.msg.in.event.InTemplateMsgEvent;
import com.jfinal.weixin.sdk.msg.in.speech_recognition.InSpeechRecognitionResults;
import com.jfinal.weixin.sdk.utils.JsonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author fn
 * @description 公众号,增加响应需求需要重写相应方法
 * @date 2023/3/18 4:16
 */
@FnMsg("/weixin/wx")
@RequiredArgsConstructor
@Slf4j
public class WxController extends FnMsgControllerAdapter {
    /************************************************************************************************************
     ***************************************        关注/取消关注事件   ********************************************
     *************************************************************************************************************/
    @Override
    protected void processInFollowEvent(InFollowEvent inFollowEvent) {
        //处理接收到的关注/取消关注事件
        /**   看场景扩展即可
         * 1、可以把用户数据保存到数据库
         * 2、给用户发送订阅消息
         * 3、给用户发送模板消息
         */

    }

    /************************************************************************************************************
     ***************************************        模板消息   ********************************************
     *************************************************************************************************************/

    @Override
    protected void processInTemplateMsgEvent(InTemplateMsgEvent inTemplateMsgEvent) {
        super.processInTemplateMsgEvent(inTemplateMsgEvent);
        //发送处在ApiController
        //成功发送模板消息走此逻辑
        log.info("模板消息：{}", JsonUtils.toJson(inTemplateMsgEvent));
    }

    /************************************************************************************************************
     ***************************************        菜单事件  ********************************************
     *************************************************************************************************************/
    @Override
    protected void processInMenuEvent(InMenuEvent inMenuEvent) {
        //处理接收到的自定义菜单事件
    }

    /************************************************************************************************************
     ***************************************    用户消息与客服回复  ********************************************
     *************************************************************************************************************/

    @Override
    protected void processInTextMsg(InTextMsg inTextMsg) {
        //公众号发送文字时此处监听
        log.info("收到用户文本消息：{}", JsonUtils.toJson(inTextMsg));
        //接受文本消息回复用户文本消息,可以将数据保存到数据库进行管理。再调用指定接口回复用户，比如/reply/text/msg
        if ("1".equals(inTextMsg.getContent())) {
            CustomServiceApi.sendText(inTextMsg.getFromUserName(), "2");
        } else {
            CustomServiceApi.sendText(inTextMsg.getFromUserName(), "您输入1试试");
        }
    }

    @Override
    protected void processInImageMsg(InImageMsg inImageMsg) {
        CustomServiceApi.sendImage(inImageMsg.getFromUserName(), inImageMsg.getMediaId());
    }

    @Override
    protected void processInSpeechRecognitionResults(InSpeechRecognitionResults inSpeechRecognitionResults) {
        renderDefault();
    }
    @Override
    protected void processInVoiceMsg(InVoiceMsg inVoiceMsg) {
        super.processInVoiceMsg(inVoiceMsg);
    }

    @Override
    protected void processInVideoMsg(InVideoMsg inVideoMsg) {
        super.processInVideoMsg(inVideoMsg);
    }

    @Override
    protected void processInLocationMsg(InLocationMsg inLocationMsg) {
        super.processInLocationMsg(inLocationMsg);
    }

    @Override
    protected void processInLinkMsg(InLinkMsg inLinkMsg) {
        super.processInLinkMsg(inLinkMsg);
    }

}
